package blog.controller;

import blog.common.Result;
import blog.entity.Comment;
import blog.service.CommentService;
import blog.service.VideoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/comment")
@Slf4j
public class CommentController {
    @Autowired
   private CommentService commentService;

    @Autowired
        private VideoService videoService;

    /*
    *添加评论
    * http://localhost:8080/common
     */
    @PostMapping
    public Result addComment(@RequestBody Comment comment){
        if (StringUtils.isEmpty(comment.getContent())){
            return Result.error("评论内容不能为空");
        }
        boolean save = commentService.save(comment);
        if (!save){
            return Result.error("评论失败");
        }
        return Result.success("评论成功");

    }
    /*
    *获取评论
    * http://localhost:8080/comment?uid=1&videoName=你的名字&beginTime=2002/4/15&endTime=2023/5/15
    * uid为空时，获取所有评论，后面的参数是筛选条件
     */
    @GetMapping
    public Result getComment(@RequestParam(value = "uid",required = false) Integer uid,
                             @RequestParam("page") Integer page, @RequestParam("pageSize") Integer pageSize,
                             @RequestParam(value = "vid",required = false) Integer vid,
                             @RequestParam(value = "videoName",required = false) String videoName,
                             @RequestParam(value = "comment",required = false) String comment,
                             @RequestParam(value = "beginTime",required = false) String beginTime,
                             @RequestParam(value = "endTime",required = false) String endTime){
        LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();
        if (uid != null){
            wrapper.eq(Comment::getUid,uid);
        }
        if (videoName != null){
            if (videoService.getVideoIdList(videoName)==null)
            {
                return Result.error("没有该视频");
            }
            wrapper.in(Comment::getVid,videoService.getVideoIdList(videoName));

        }
        if (beginTime != null){
            wrapper.ge(Comment::getCreateTime,beginTime);
        }
        if (endTime != null){
            wrapper.le(Comment::getCreateTime,endTime);
        }
        if (vid != null){
            wrapper.eq(Comment::getVid,vid);
        }
        if (comment != null){
            wrapper.like(Comment::getContent,comment);
        }

        //按照时间降序排列
        wrapper.orderByDesc(Comment::getCreateTime);
        Page<Comment> commentPage = commentService.page(new Page<>(page, pageSize), wrapper);


        return Result.success(commentPage);
    }

    /*
    *删除评论
    * http://localhost:8080/comment/1
     */
    @DeleteMapping("/{id}")
    public Result deleteComment(@PathVariable("id") Integer id){
        boolean remove = commentService.removeById(id);
        if (!remove){
            return Result.error("删除失败");
        }
        return Result.success("删除成功");
    }







}